package com.sxkf.template.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.sxkf.template.entity.Product;
import com.sxkf.template.entity.SpellOut;
import com.sxkf.template.mapper.ProductMapper;
import com.sxkf.template.mapper.SpellOutMapper;
import com.sxkf.template.mq.Producer;
import com.sxkf.template.service.SpellOutService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.SxkfException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品拼团 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-02 18:10:34
 */
@Service
public class SpellOutServiceImpl extends ServiceImpl<SpellOutMapper, SpellOut> implements SpellOutService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private Producer producer;

    @Override
    public String create(SpellOut spellOut) {
        Product product = productMapper.selectById(spellOut.getProductId());
        checkProduct(product);
        spellOut.setNeedCount(product.getNeedCount());
        spellOut.setState("0");
        spellOut.setJoinCount(1);
        spellOut.setJoinUserList(spellOut.getCreateId());
        // 这里需要加入MQ延时队列处理到期结束拼团的逻辑
        DateTime endTime = DateUtil.offset(new Date(), DateField.MINUTE, product.getDuration());
        spellOut.setEndTime(endTime);
        save(spellOut);
        int second = product.getDuration()*60;
        // 如果有时间延迟问题启用这里。理论上用不到了。
//        second = (int)DateUtil.between(endTime, new Date(), DateUnit.SECOND);
        // 延迟
        producer.sendWithDelay(spellOut.getId(),second);
        return spellOut.getId();
    }

    @Override
    public void join(String spellOutId,String userId) {
        SpellOut spellOut = getById(spellOutId);
        // 自己创建的团直接返回，不做参团操作。
        if (spellOut.getCreateId().equals(userId)) {
            return;
        }

        List<String> userIdList = Arrays.asList(spellOut.getJoinUserList().split(","));
        if (userIdList.contains(userId)) {
            throw new SxkfException("不能重复参团");
        }

        // 下面可能发生的问题解决方案
//        List<String> joinUsers = checkJoin(userId, spellOut);
//        String userIdStr = joinUsers.toString();
//        String userListStr = userIdStr.substring(1, userIdStr.length() - 1).replaceAll(" ", "");
//        spellOut.setJoinUserList(userListStr + "," + userId);

        // 可能存在重复参与拼团问题
        spellOut.setJoinUserList(spellOut.getJoinUserList()+","+userId);
        spellOut.setJoinCount(spellOut.getJoinCount()+1);

        if (spellOut.getJoinCount()>=spellOut.getNeedCount()){
            //已拼成
            spellOut.setState("1");
        }
        updateById(spellOut);
    }

    @Override
    public List<String> checkJoin(String userId, SpellOut spellOut) {
        if (spellOut.getEndTime().compareTo(new Date())<0){
            throw new SxkfException("该团已过期");
        }
        if ("1".equals(spellOut.getState())){
            throw new SxkfException("该团已拼成");
        }
        List<String> joinUsers = Arrays.asList(spellOut.getJoinUserList().split(","));
        if (joinUsers.contains(userId)){
            throw new SxkfException("不可重复参与拼团");
        }
        return joinUsers;
    }

    @Override
    public void checkProduct(Product product) {
        if (product == null){
            throw new SxkfException("商品异常");
        }
        if ("0".equals(product.getIsTeam())){
            throw new SxkfException("商品不支持拼团");
        }
    }
}
